package hut.ai.team.inf;

import com.badlogic.gdx.Input.Buttons;
import com.badlogic.gdx.InputAdapter;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Stage;

public class ClickInput extends InputAdapter {
	private Stage stage;

	public ClickInput(Stage stage) {
		this.stage = stage;
	}

	private Vector2 tmp = new Vector2();
	private IClickable previous = null;
	@Override
	public boolean touchDown(int screenX, int screenY, int pointer, int button) {
		if (button == Buttons.LEFT) {
			tmp.set(stage.screenToStageCoordinates(tmp.set(screenX, screenY)));
			Actor actor = stage.hit(tmp.x, tmp.y, true);
			if (actor instanceof IClickable) {
				IClickable btn = (IClickable) actor;
				btn.onTouchDown();
				previous = btn;
			}
		}
		return super.touchDown(screenX, screenY, pointer, button);
	}

	@Override
	public boolean touchUp(int screenX, int screenY, int pointer, int button) {
		if (button == Buttons.LEFT) {
			tmp.set(stage.screenToStageCoordinates(tmp.set(screenX, screenY)));
			Actor actor = stage.hit(tmp.x, tmp.y, true);
			if (actor instanceof IClickable) {
				IClickable btn = (IClickable) actor;
				if(btn==previous){
					btn.onTouchUp();
					previous = null;
				}
			}
		}
		return super.touchUp(screenX, screenY, pointer, button);
	}
}

